package com.kafka.sync2;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.util.Properties;

/**
 * @author xq
 * @Description: 定义解析命令行
 * @date: 2020/6/1115:01
 */
public class CliDefinition {

    public static Properties getConfig(String[] args) {
        final Options options = new Options();
        Option na = new Option("na", "name", true, "任务名");
        // 源kafka
        Option sau = new Option("sau", "s_auth", false, "是否开启权限认证");
        Option skc = new Option("skc", "s_krb5Conf", true, "krb5Conf");
        Option sjc = new Option("sjc", "s_jaasConf", true, "jaasConf");
        Option sbu = new Option("sbu", "s_brokeUrl", true, "kafka.brokeUrl");
        Option sto = new Option("sto", "s_topic", true, "kafka.topic");
        Option scg = new Option("scg", "s_consumerGroup", true, "kafka.consumerGroup");
        // 目标kafka
        Option tau = new Option("tau", "t_auth", false, "是否开启权限认证");
        Option tkc = new Option("tkc", "t_krb5Conf", true, "krb5Conf");
        Option tjc = new Option("tjc", "t_jaasConf", true, "jaasConf");
        Option tbu = new Option("tbu", "t_brokeUrl", true, "kafka.brokeUrl");
        Option tto = new Option("tto", "t_topic", true, "kafka.topic");

        Option rc = new Option("rc", "retry", true, "db.max.retryCount");
        Option fc = new Option("fc", "fail", true, "db.max.failCount");

        Option fb = new Option("fb", "file", true, "文件备份");

        options.addOption(na);
        options.addOption(sau).addOption(skc).addOption(sjc).addOption(sbu).addOption(sto).addOption(scg);
        options.addOption(tau).addOption(tkc).addOption(tjc).addOption(tbu).addOption(tto);
        options.addOption(rc).addOption(fc).addOption(fb);

        CommandLineParser parser = new DefaultParser();
        CommandLine cl;
        try {
            cl = parser.parse(options, args);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("命令行解析失败");
        }

        Properties config = new Properties();
        for (Config c : Config.values()) {
            String longOpt = c.value();
            if (cl.hasOption(longOpt)) {
                if (longOpt.equals("s_auth") || longOpt.equals("t_auth")) {
                    config.put(longOpt, true);
                    continue;
                }
                config.put(c.value(), cl.getOptionValue(c.value()));
            }
        }
        return config;
    }

}
